<?php

/**
 * @author    1054229380@qq.com
 * @since    2017-7-3
 * @desc 日志类
 **/
class logs
{
    /**
     * 日志写入
     **/
    public static function write($logArr)
    {
        $errorStr = $sql = $fileConent = $postData = null;
        if (is_array($logArr)) {
            $errorStr = $logArr['errorMsg'];
            $sql = $logArr['sql'];
            $postData = json_encode($logArr['postData']);
        } else {
            $errorStr = $logArr;
        }
        $currTime = date("Y-m-d H:i:s");
        $httpUrl = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?' . $_SERVER["QUERY_STRING"];

        $folName = date("Ym", strtotime($currTime));

        $folUrl = APP_ROOT . APP_LOGDIR . "/$folName";

        if (!is_dir($folUrl)) {
            mkdir($folUrl, 0700);
            chmod($folUrl, 0777);
        }


        $fileName = date("Y-m-d-H", strtotime($currTime));
        $fileUrl = APP_ROOT . APP_LOGDIR . "/$folName/log-{$fileName}.txt";

        if (file_exists($fileUrl)) {
            $fileConent = file_get_contents($fileUrl);
        }
        if ($fileConent != "") {
            $fileConent = $fileConent . "\r\n========================================\r\n";
        }
        $fileConent = $fileConent . "CreateTime:" . $currTime;
        $fileConent = $fileConent . "\r\nhttpUrl:" . $httpUrl;
        if ($sql != "") {
            $fileConent = $fileConent . "\r\nsql:" . $sql;
        }
        $fileConent = $fileConent . "\r\nerror:" . $errorStr;
        $fileConent = $fileConent . "\r\npostData:" . $postData;

        file_put_contents($fileUrl, $fileConent);
    }
}

?>